Use case: Communication with FIFO monitoring (polling)

The changes between HAL1 and HAL2 are limited to naming; the overall programming model remains unchanged. For transmission, add messages to the TX FIFO. For reception, first monitor the RX FIFO level, and when a message is received, read it from the FIFO.

Sequence diagram

  @startuml
            participant "User application" as App
  participant "<font color=black><b>HAL FDCAN"  as FDCAN #19DD3A

  Note over App#lightyellow: Prepare the Tx Header \n \n

  == FDCAN HAL Message Transmit  ==
  App-> FDCAN: HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan, &TxHeader, TxData[0])
  FDCAN --> App:<color green>HAL_OK
  == FDCAN HAL Message Receive  ==
  loop#darkgrey  if fifo_level != 1
     App -> FDCAN: HAL_FDCAN_GetRxFifoFillLevel()
     note right
        Retrieve the Rx FIFO fill level
     endnote

     FDCAN --> App: fifo_level
  end

  App -> FDCAN: HAL_FDCAN_GetRxMessage()
  note right
     Retrieve an FDCAN frame from the Rx FIFO
  endnote

  FDCAN --> App:<color green>HAL_OK

  @enduml

Code snippet

HAL1

HAL2

/* Prepare FDCAN transmit message header */
TxHeader.Identifier          = 0x111;
TxHeader.IdType              = FDCAN_STANDARD_ID;
TxHeader.TxFrameType         = FDCAN_DATA_FRAME;
TxHeader.DataLength          = FDCAN_DLC_BYTES_8;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch       = FDCAN_BRS_ON;
TxHeader.FDFormat            = FDCAN_FD_CAN;
TxHeader.TxEventFifoControl  = FDCAN_NO_TX_EVENTS;
TxHeader.MessageMarker       = 0;

HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan, &TxHeader, &TxData[0]);

while(HAL_FDCAN_GetRxFifoFillLevel(&hfdcan, FDCAN_RX_FIFO0) < 1);

HAL_FDCAN_GetRxMessage(&hfdcan, FDCAN_RX_FIFO0, &RxHeader, RxData);
/* Prepare FDCAN transmit message header */
tx_element_header.b.identifier            = 0x111,
tx_element_header.b.identifier_type       = HAL_FDCAN_ID_STANDARD,
tx_element_header.b.frame_type            = HAL_FDCAN_FRAME_DATA,
tx_element_header.b.data_length           = HAL_FDCAN_DATA_LEN_CAN_FDCAN_8_BYTE,
tx_element_header.b.error_state_indicator = HAL_FDCAN_ERROR_STATE_IND_ACTIVE,
tx_element_header.b.bit_rate_switch       = HAL_FDCAN_BIT_RATE_SWITCH_ON,
tx_element_header.b.frame_format          = HAL_FDCAN_HEADER_FRAME_FORMAT_FD_CAN,
tx_element_header.b.event_fifo_control    = HAL_FDCAN_FIFO_STORE_TX_EVENTS,
tx_element_header.b.message_marker        = 0U,

HAL_FDCAN_ReqTransmitMsgFromFIFOQ(pFDCAN, &tx_element_header, pTxData);

while(HAL_FDCAN_GetRxFifoFillLevel(pFDCAN, HAL_FDCAN_RX_FIFO_0, &rx_elements) < 1);

HAL_FDCAN_GetReceivedMessage(pFDCAN, HAL_FDCAN_RX_FIFO_0, &rx_element_header, RxBuffer);

Key changes

  1. In HAL2, the transmit API is HAL_FDCAN_ReqTransmitMsgFromFIFOQ() (instead of HAL_FDCAN_AddMessageToTxFifoQ()).

  2. In HAL2, the receive API is HAL_FDCAN_GetReceivedMessage() (instead of HAL_FDCAN_GetRxMessage()).